﻿using System;
using System.IO;
using System.Collections.Generic;
using AttachmentModule.Dime;
using System.Xml;
using System.Xml.XPath;
using Coordenadas;
using System.Web.Services.Protocols;
namespace LectorXML
{
    public class XMLCoordenadasLocal:XMLCoordenadas
    {
        private string url;
        private Central.Central proxy;
        private List<LatLong> lugares;
        private List<LatLong> carros;

        public XMLCoordenadasLocal(string url)
        {
            this.url = url;
            this.lugares = new List<LatLong>();
            this.carros = new List<LatLong>();
            this.proxy = new Central.Central(url);
        }
        public List<LatLong> Emergencias()
        {
            XmlNode xn;
            try
            {
                xn = this.proxy.Emergencias();
            }
            catch (Exception) {
                try
                {
                    xn = this.proxy.Emergencias();
                }
                catch (Exception)
                {
                    xn = null;
                    return new List<LatLong>();
                }
            }
            XmlNodeList nl = xn.SelectNodes("//emergencia");
            try
            {

                string separadorDecimal = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
                for (int i = 0; i < nl.Count; i++)
                {
                    this.lugares.Add(new LatLong(
                        double.Parse(nl[i].Attributes["lat"].Value.Replace(",", separadorDecimal)),
                        double.Parse(nl[i].Attributes["lon"].Value.Replace(",", separadorDecimal)),
                        nl[i].Attributes["lugar"].Value
                    ));
                }
            }
            catch (Exception)
            {}
            return this.lugares;
        }
        public List<LatLong> CarrosActualiza(string nombre, string lat, string lon)
        {
            XmlNode xn;
            try
            {
                xn = this.proxy.CarrosActualiza(nombre, lat, lon);
            }
            catch (Exception)
            {
                try
                {
                    xn = this.proxy.CarrosActualiza(nombre, lat, lon);
                }
                catch (Exception)
                {
                    xn = null;
                    return new List<LatLong>();
                }
            }
            if (xn != null)
            {
                this.carros.Clear();

                XmlNodeList listaCarros = xn.SelectNodes("//carro");
                for (int i = 0; i < listaCarros.Count; i++)
                {
                    if (listaCarros[i].SelectNodes("posicion/lat")[0].InnerText != "" && listaCarros[i].SelectNodes("posicion/lat")[0].InnerText != "")
                    {
                        this.carros.Add(new LatLong(
                            double.Parse(listaCarros[i].SelectNodes("posicion/lat")[0].InnerText),
                            double.Parse(listaCarros[i].SelectNodes("posicion/lon")[0].InnerText),
                            listaCarros[i].SelectNodes("nombre")[0].InnerText
                        ));
                    }
                }
            }
            return this.carros;
        }

        public List<LatLong> Carros()
        {
            XmlNode xn;
            try
            {
                xn = this.proxy.Carros();
            }
            catch (Exception)
            {
                try
                {
                    xn = this.proxy.Carros();
                }
                catch (Exception)
                {
                    xn = null;
                    return new List<LatLong>();
                }
            }
            if (xn != null)
            {
                this.carros.Clear();

                XmlNodeList listaCarros = xn.SelectNodes("//carro");
                for (int i = 0; i < listaCarros.Count; i++)
                {
                    if (listaCarros[i].SelectNodes("posicion/lat")[0].InnerText != "" && listaCarros[i].SelectNodes("posicion/lat")[0].InnerText != "")
                    {
                        this.carros.Add(new LatLong(
                            double.Parse(listaCarros[i].SelectNodes("posicion/lat")[0].InnerText),
                            double.Parse(listaCarros[i].SelectNodes("posicion/lon")[0].InnerText),
                            listaCarros[i].SelectNodes("nombre")[0].InnerText
                        ));
                    }
                }
            }
            return this.carros;
        }
    }
}
